﻿using System.Collections.Generic;
using System.Drawing;

namespace Dot5Demo
{
    public abstract class Render : IRender
    {
        public bool Enabled { get; set; }
        public Point Location { get; set; }
        public IList<IRender> Renders { get; set; }
        public Size Size { get; set; }
        public Color BackCOlor { get; set; }
        public Color ForeCOlor { get; set; }
        public Font Font { get; set; }
        public bool Visible { get; set; }

        public void AddRenderer(IRender renderer)
        {
            if (!Exist(renderer))
                return;
            Renders.Add(renderer);
        }

        public void ClearRenderer()
        {
            if (Renders == null || Renders.Count == 0)
                return;
            Renders.Clear();
        }


        public bool Exist(IRender renderer)
        {
            if (renderer == null)
                return false;
            if (Renders == null || Renders.Count == 0)
                return false;
            return Renders.Contains(renderer);
        }
        public virtual void Dispose()
        {
            Font?.Dispose();
        }

        public virtual void Invalidate(Rectangle rc)
        {
        }

        public void RemoveRenderer(IRender renderer)
        {
            if (!Exist(renderer))
                return;
            Renders.Remove(renderer);
        }

        public virtual void DoRender(Graphics g, Rectangle rect)
        {
            g.Clip = new Region(rect);
            using (var brush = new SolidBrush(BackCOlor))
            {
                g.FillRectangle(brush, new Rectangle(Location, Size));
            }
            g.ResetClip();
        }

        public virtual void DoRender(Graphics g, RectangleF rectf)
        {
            g.Clip = new Region(rectf);
            using (var brush = new SolidBrush(BackCOlor))
            {
                g.FillRectangle(brush, new Rectangle(Location, Size));
            }
            g.ResetClip();
        }
    }
}